﻿using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using GrayParrot.Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UTInfrastructure
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestFacadeBaseLogAttribute()
        {
            TestFacade m_tf = new TestFacade();
            //m_tf.DoWithTransaction();
            m_tf.DoWorkInternal(1, new Prova1() { ProvaP1=10, ProvaP2=11 });

            string mm = m_tf.LastException.Message;

            Console.WriteLine(mm);
        }

        [TestMethod]
        public void TestTraceAttribute()
        {
            TestFacade m_tf = new TestFacade();
            m_tf.TestTraceAttribute(); 
        }

        [TestMethod]
        public void TestLogAttribute()
        {
            TestLog m_tf = new TestLog();
            m_tf.DoWorkInternal(1, new Prova1() { ProvaP1 = 10, ProvaP2 = 11 });
        }

        [TestMethod]
        public void TestDbEntityValidationException()
        {
            TestLog m_tf = new TestLog();
            m_tf.TestDbEntityValidationException();

            TestLog m_tf2 = new TestLog();
            m_tf2.TestDbEntityValidationException2(10, 20);


            GrayParrot.Logging.Logger.For(typeof(UnitTest1)).Error(" ------- ");
        }

        [TestCleanup]
        public void Cleanup()
        {
            CallContext.FreeNamedDataSlot("log4net.Util.LogicalThreadContextProperties");
        }
    }

    public class TestLog
    {
        public TestLog()
        {

        }

        [WithExceptionHandling]
        public void DoWorkInternal(int primo, Prova1 secondo)
        {
            Console.WriteLine("Linea1");
            Console.WriteLine("Linea2");

            int a = 10;
            int b = 0;

            int c = a / b;
        }

        [WithExceptionHandling]
        public void TestDbEntityValidationException()
        {
            System.Data.Entity.Validation.DbEntityValidationException p1 = new System.Data.Entity.Validation.DbEntityValidationException("prova DbEntityValidationException1"); 
            System.Data.Entity.Validation.DbEntityValidationException p = new System.Data.Entity.Validation.DbEntityValidationException("prova DbEntityValidationException", p1);

            throw p;
        }

        [WithExceptionHandling]
        public void TestDbEntityValidationException2(int p1, int p2)
        {
            System.Data.Entity.Validation.DbEntityValidationException ppp1 = new System.Data.Entity.Validation.DbEntityValidationException("prova DbEntityValidationException1");
            System.Data.Entity.Validation.DbEntityValidationException pppp = new System.Data.Entity.Validation.DbEntityValidationException("prova DbEntityValidationException", ppp1);

            throw pppp;
        }
    }

    public class TestFacade : FacadeBase
    {
        public TestFacade()
        {

        }

        [WithExceptionHandling]
        public void DoWorkInternal(int primo, Prova1 secondo)
        {
            Console.WriteLine("Linea1");
            Console.WriteLine("Linea2");

            int a = 10;
            int b = 0;

            int c = a / b;
        }

        public void DoWorkInternal2()
        {
            Console.WriteLine("Linea1");
            Console.WriteLine("Linea2");

            int a = 10;
            int b = 0;

            int c = a / b;
        }

        [WithTrace]
        public void TestTraceAttribute()
        {
            Thread.Sleep(5);
        }
    }

    [Serializable]
    public class Prova1
    {
        public int ProvaP1 { get; set; }
        public int ProvaP2 { get; set; }

        public Prova1()
        {

        }
    }
}
